﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Windows.Threading;
namespace WPFExcelReport.Chart
{
    public class ChartExViewModel : ViewModelBase
    {
        SETINTEXDB INTEXDB;
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        private bool _IsSleep = false;
        public bool IsSleep
        {
            get
            {
                return _IsSleep;
            }
            set
            {
                _IsSleep = value;
                OnPropertyChanged("IsSleep", value);
            }
        } 
        public double DefaultConvert = 1;  
        public List<double> FontSizes { get; set; }
        public List<double> DoughnutInnerRadiusRatios { get; set; }
        public Dictionary<string, De.TorstenMandelkow.MetroChart.ResourceDictionaryCollection> Palettes { get; set; }
        public List<string> SelectionBrushes { get; set; } 
        private object selectedPalette = null;
        public object SelectedPalette
        {
            get
            {
                return selectedPalette;
            }
            set
            {
                selectedPalette = value;
               OnPropertyChanged("SelectedPalette",value);
            }
        }

        private bool darkLayout = false;
        public bool DarkLayout
        {
            get
            {
                return darkLayout;
            }
            set
            {
                darkLayout = value;
                OnPropertyChanged("DarkLayout",value);
                OnPropertyChanged("Foreground", value);
                OnPropertyChanged("Background", value);
                OnPropertyChanged("MainBackground", value);
                OnPropertyChanged("MainForeground", value);
            }
        }

        public string Foreground
        {
            get
            {
                if (darkLayout)
                {
                    return "#FFEEEEEE";
                }
                return "#FF666666";
            }
        }
        public string MainForeground
        {
            get
            {
                if (darkLayout)
                {
                    return "#FFFFFFFF";
                }
                return "#FF666666";
            }
        }
        public string Background
        {
            get
            {
                if (darkLayout)
                {
                    return "#FF333333";
                }
                return "#FFF9F9F9";
            }
        }
        public string MainBackground
        {
            get
            {
                if (darkLayout)
                {
                    return "#FF000000";
                }
                return "#FFEFEFEF";
            }
        }

        public string ToolTipFormat
        {
            get
            {
                return "{0} in series '{2}' has value '{1}' ({3:P2})";
            }
        }
        private string selectedBrush = null;
        public string SelectedBrush
        {
            get
            {
                return selectedBrush;
            }
            set
            {
                selectedBrush = value;
                OnPropertyChanged("SelectedBrush", value);
            }
        }

        private double selectedDoughnutInnerRadiusRatio = 0.75;
        public double SelectedDoughnutInnerRadiusRatio
        {
            get
            {
                return selectedDoughnutInnerRadiusRatio;
            }
            set
            {
                selectedDoughnutInnerRadiusRatio = value;
                OnPropertyChanged("SelectedDoughnutInnerRadiusRatio", value);
                OnPropertyChanged("SelectedDoughnutInnerRadiusRatioString", value);
            }
        }

        public string SelectedDoughnutInnerRadiusRatioString
        {
            get
            {
                return String.Format("{0:P1}.", SelectedDoughnutInnerRadiusRatio);
            }
        }
        private bool _IsMaster; public bool IsMaster { get { return _IsMaster; } set { _IsMaster = value; OnPropertyChanged("IsMaster", value); } }
        private bool _IsChart00; public bool IsChart00 { get { return _IsChart00; } set { _IsChart00 = value; OnPropertyChanged("IsChart00", value); } }
        private bool _IsChart01; public bool IsChart01 { get { return _IsChart01; } set { _IsChart01 = value; OnPropertyChanged("IsChart01", value); } }
        private bool _IsChart02; public bool IsChart02 { get { return _IsChart02; } set { _IsChart02 = value; OnPropertyChanged("IsChart02", value); } }
        private bool _IsChart03; public bool IsChart03 { get { return _IsChart03; } set { _IsChart03 = value; OnPropertyChanged("IsChart03", value); } }
        private bool _IsChart04; public bool IsChart04 { get { return _IsChart04; } set { _IsChart04 = value; OnPropertyChanged("IsChart04", value); } }
        private bool _IsChart05; public bool IsChart05 { get { return _IsChart05; } set { _IsChart05 = value; OnPropertyChanged("IsChart05", value); } }
        private bool _IsChart06; public bool IsChart06 { get { return _IsChart06; } set { _IsChart06 = value; OnPropertyChanged("IsChart06", value); } }
        private bool _IsChart07; public bool IsChart07 { get { return _IsChart07; } set { _IsChart07 = value; OnPropertyChanged("IsChart07", value); } }
        private bool _IsChart08; public bool IsChart08 { get { return _IsChart08; } set { _IsChart08 = value; OnPropertyChanged("IsChart08", value); } }
        private bool _IsChart09; public bool IsChart09 { get { return _IsChart09; } set { _IsChart09 = value; OnPropertyChanged("IsChart09", value); } }
        private string _S1; public string S1 { get { return _S1; } set { _S1 = value; OnPropertyChanged("S1", value); } }
        private string _ChartTitle1; public string ChartTitle1 { get { return _ChartTitle1; } set { _ChartTitle1 = value; OnPropertyChanged("ChartTitle1", value); } }
        private string _ChartSubTitle1; public string ChartSubTitle1 { get { return _ChartSubTitle1; } set { _ChartSubTitle1 = value; OnPropertyChanged("ChartSubTitle1", value); } }
        private string _DefaultTitle; public string DefaultTitle { get { return _DefaultTitle; } set { _DefaultTitle = value; OnPropertyChanged("DefaultTitle", value); } }
        private string _DefaultSubTitle; public string DefaultSubTitle { get { return _DefaultSubTitle; } set { _DefaultSubTitle = value; OnPropertyChanged("DefaultSubTitle", value); } }

        private string _ChartTitle; public string ChartTitle { get { return _ChartTitle; } set { _ChartTitle = value; OnPropertyChanged("ChartTitle", value); } } 
        private string _ChartSubTitle; public string ChartSubTitle { get { return _ChartSubTitle; } set { _ChartSubTitle = value; OnPropertyChanged("ChartSubTitle", value); } } 
        private string _S2; public string S2 { get { return _S2; } set { _S2 = value; OnPropertyChanged("S2", value); } }
        private string _ChartTitle2; public string ChartTitle2 { get { return _ChartTitle2; } set { _ChartTitle2 = value; OnPropertyChanged("ChartTitle2", value); } }
        private string _ChartSubTitle2; public string ChartSubTitle2 { get { return _ChartSubTitle2; } set { _ChartSubTitle2 = value; OnPropertyChanged("ChartSubTitle2", value); } }
        private string _S3; public string S3 { get { return _S3; } set { _S3 = value; OnPropertyChanged("S3", value); } }
        private string _ChartTitle3; public string ChartTitle3 { get { return _ChartTitle3; } set { _ChartTitle3 = value; OnPropertyChanged("ChartTitle3", value); } }
        private string _ChartSubTitle3; public string ChartSubTitle3 { get { return _ChartSubTitle3; } set { _ChartSubTitle3 = value; OnPropertyChanged("ChartSubTitle3", value); } }
        private string _S4; public string S4 { get { return _S4; } set { _S4 = value; OnPropertyChanged("S4", value); } }
        private string _ChartTitle4; public string ChartTitle4 { get { return _ChartTitle4; } set { _ChartTitle4 = value; OnPropertyChanged("ChartTitle4", value); } }
        private string _ChartSubTitle4; public string ChartSubTitle4 { get { return _ChartSubTitle4; } set { _ChartSubTitle4 = value; OnPropertyChanged("ChartSubTitle4", value); } }
        private string _S5; public string S5 { get { return _S5; } set { _S5 = value; OnPropertyChanged("S5", value); } }
        private string _ChartTitle5; public string ChartTitle5 { get { return _ChartTitle5; } set { _ChartTitle5 = value; OnPropertyChanged("ChartTitle5", value); } }
        private string _ChartSubTitle5; public string ChartSubTitle5 { get { return _ChartSubTitle5; } set { _ChartSubTitle5 = value; OnPropertyChanged("ChartSubTitle5", value); } }
        private bool _IsS1; public bool IsS1 { get { return _IsS1; } set { _IsS1 = value; OnPropertyChanged("IsS1", value); } }
        private bool _IsS2; public bool IsS2 { get { return _IsS2; } set { _IsS2 = value; OnPropertyChanged("IsS2", value); } }
        private bool _IsS3; public bool IsS3 { get { return _IsS3; } set { _IsS3 = value; OnPropertyChanged("IsS3", value); } }
        private bool _IsS4; public bool IsS4 { get { return _IsS4; } set { _IsS4 = value; OnPropertyChanged("IsS4", value); } }
        private bool _IsS5; public bool IsS5 { get { return _IsS5; } set { _IsS5 = value; OnPropertyChanged("IsS5", value); } }
        private bool _IsRowColumnSwitched; public bool IsRowColumnSwitched { get { return _IsRowColumnSwitched; } set { _IsRowColumnSwitched = value; OnPropertyChanged("IsRowColumnSwitched", value); } }
        private bool _IsLegendVisible; public bool IsLegendVisible { get { return _IsLegendVisible; } set { _IsLegendVisible = value; OnPropertyChanged("IsLegendVisible", value); } }
        private bool _IsTitleVisible; public bool IsTitleVisible { get { return _IsTitleVisible; } set { _IsTitleVisible = value; OnPropertyChanged("IsTitleVisible", value); } }

        

        private double fontSize = 11.0;
        public double SelectedFontSize
        {
            get
            {
                return fontSize;
            }
            set
            {
                fontSize = value;
                OnPropertyChanged("SelectedFontSize", value);
                OnPropertyChanged("SelectedFontSizeString", value);
            }
        }

        public string SelectedFontSizeString
        {
            get
            {
                return SelectedFontSize.ToString() + "px";
            }
        }
        
        private ObservableCollection<ChartClass2> _DATALH; public ObservableCollection<ChartClass2> DATALH { get { return _DATALH; } set { _DATALH = value; OnPropertyChanged("DATALH", value); } }
        private ObservableCollection<ChartClass> _DATA1; public ObservableCollection<ChartClass> DATA1 { get { return _DATA1; } set { _DATA1 = value; OnPropertyChanged("DATA1", value); } }
        private ObservableCollection<ChartClass> _DATA2; public ObservableCollection<ChartClass> DATA2 { get { return _DATA2; } set { _DATA2 = value; OnPropertyChanged("DATA2", value); } }
        private ObservableCollection<ChartClass> _DATA3; public ObservableCollection<ChartClass> DATA3 { get { return _DATA3; } set { _DATA3 = value; OnPropertyChanged("DATA3", value); } }
        private ObservableCollection<ChartClass> _DATA4; public ObservableCollection<ChartClass> DATA4 { get { return _DATA4; } set { _DATA4 = value; OnPropertyChanged("DATA4", value); } }
        private ObservableCollection<ChartClass> _DATA5; public ObservableCollection<ChartClass> DATA5 { get { return _DATA5; } set { _DATA5 = value; OnPropertyChanged("DATA5", value); } }
        private bool canUpdate = true;
        public double Converter
        {
            get;
            set;
        }
        public double RATIO
        {
            get;
            set;
        }
        public bool CanUpdate
        {
            get { return canUpdate; }
            set { canUpdate = value; }
        }

        private TimeSpan updateSpeed = TimeSpan.FromMilliseconds(10);

        public TimeSpan UpdateSpeed
        {
            get { return updateSpeed; }
            set
            {

                updateSpeed = value;
                timer.Interval = updateSpeed;
            }
        } 
       
        public int MaxData { get; set; }
        private object selectedItem = null;
        public object SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                selectedItem = value;
                OnPropertyChanged("SelectedItem", value);
            }
        }
        private List<SeriesData> _Series; public List<SeriesData> Series { get { return _Series; } set { _Series = value; OnPropertyChanged("Series", value); } }

        public double limitation; 
       public SeriesData SeriesData1;
       public SeriesData SeriesData2;
       public SeriesData SeriesData3;
       public SeriesData SeriesData4;
       public SeriesData SeriesData5;
       
        public ChartExViewModel()
        {
            _IsMaster = false;
            _IsChart00 = false;
            _IsChart01 = false;
            _IsChart02 = false;
            _IsChart03 = false;
            _IsChart04 = false;
            _IsChart05 = false;
            _IsChart06 = false;
            _IsChart07 = false;
            _IsChart08 = false;
            _IsChart09 = false;
            INTEXDB = new SETINTEXDB();
            if (app.User.Trim() == "ITXV"
                || app.User.Trim() == "JLTV"
                || app.User.Trim() == "YCAV")
            {
                _IsMaster = true;
            }
            Series = new List<SeriesData>();
            SetUp();
        }
        List<object> LISTDAT = new List<object>();
        private void CheckData()
        {
            if (DATA1.Count == 0) IsS1 = false;
            if (DATA2.Count == 0) IsS2 = false;
            if (DATA3.Count == 0) IsS3 = false;
            if (DATA4.Count == 0) IsS4 = false;
            if (DATA5.Count == 0) IsS5 = false;

        }
        public virtual void SetUp()
        {

            Series.Clear();
            IsSleep = false;
            IsRowColumnSwitched = true;
            IsLegendVisible = true;
            IsTitleVisible = true;
          
            FontSizes = new List<double>();
            FontSizes.Add(9.0);
            FontSizes.Add(11.0);
            FontSizes.Add(13.0);
            FontSizes.Add(18.0);
            SelectedFontSize = 11.0;

            DoughnutInnerRadiusRatios = new List<double>();
            DoughnutInnerRadiusRatios.Add(0.90);
            DoughnutInnerRadiusRatios.Add(0.75);
            DoughnutInnerRadiusRatios.Add(0.5);
            DoughnutInnerRadiusRatios.Add(0.25);
            DoughnutInnerRadiusRatios.Add(0.1);
            SelectedDoughnutInnerRadiusRatio = 0.75;

            SelectionBrushes = new List<string>();
            SelectionBrushes.Add("Orange");
            SelectionBrushes.Add("Red");
            SelectionBrushes.Add("Yellow");
            SelectionBrushes.Add("Blue");
            SelectionBrushes.Add("[NoColor]");
            SelectedBrush = SelectionBrushes.FirstOrDefault();

            #region DATALH
            DATALH = new ObservableCollection<ChartClass2>();
            ChartClass2 ChartClass2 = new ChartClass2();
            ChartClass2.Category = "2000";
            ChartClass2.High = 200;
            ChartClass2.Low = 100;
            DATALH.Add(ChartClass2);
            ChartClass2 = new ChartClass2();
            ChartClass2.Category = "2001";
            ChartClass2.High = 210;
            ChartClass2.Low = 90;
            DATALH.Add(ChartClass2);
            ChartClass2 = new ChartClass2();
            ChartClass2.Category = "2002";
            ChartClass2.High = 232;
            ChartClass2.Low = 150;
            DATALH.Add(ChartClass2);
            ChartClass2 = new ChartClass2();
            ChartClass2.Category = "2003";
            ChartClass2.High = 199;
            ChartClass2.Low = 130;
            DATALH.Add(ChartClass2); 
            ChartClass2 = new ChartClass2();
            ChartClass2.Category = "2004";
            ChartClass2.High = 100;
            ChartClass2.Low = 69;
            DATALH.Add(ChartClass2); 
            ChartClass2 = new ChartClass2();
            ChartClass2.Category = "2005";
            ChartClass2.High = 150;
            ChartClass2.Low = 110;
            DATALH.Add(ChartClass2); 
            ChartClass2 = new ChartClass2();
            ChartClass2.Category = "2006";
            ChartClass2.High = 130;
            ChartClass2.Low = 110;
            DATALH.Add(ChartClass2); 
            ChartClass2 = new ChartClass2();
            ChartClass2.Category = "2007";
            ChartClass2.High = 170;
            ChartClass2.Low = 99;
            DATALH.Add(ChartClass2);
            #endregion
            #region=======Data====
            /*timer.Tick += timer_Tick;
            timer.Interval = updateSpeed;
            timer.Start();*/
            DATA1 = new ObservableCollection<ChartClass>();
            DATA2 = new ObservableCollection<ChartClass>();
            DATA3 = new ObservableCollection<ChartClass>();
            DATA4 = new ObservableCollection<ChartClass>();
            DATA5 = new ObservableCollection<ChartClass>();
            SeriesData1 = new SeriesData();
            SeriesData1.SeriesDisplayName = "S1";
            SeriesData1.Items = DATA1;
            Series.Add(SeriesData1);
            SeriesData2 = new SeriesData();
            SeriesData2.SeriesDisplayName = "S2";
            SeriesData2.Items = DATA2;
            Series.Add(SeriesData2);
            SeriesData3 = new SeriesData();
            SeriesData3.SeriesDisplayName = "S3";
            SeriesData3.Items = DATA3;
            Series.Add(SeriesData3);
            SeriesData4 = new SeriesData();
            SeriesData4.SeriesDisplayName = "S4";
            SeriesData4.Items = DATA4;
            Series.Add(SeriesData4);
            SeriesData5 = new SeriesData();
            SeriesData5.SeriesDisplayName = "S5";
            SeriesData5.Items = DATA5;
            Series.Add(SeriesData5);
            #endregion
        }
        public virtual void ResetChart()
        {
            DATA1.Clear();
            DATA2.Clear();
            DATA3.Clear();
            DATA4.Clear();
            DATA5.Clear();
        }
        public virtual void SetUp(int iData, string DisplayName, DataTable tb, string X, string Y)
        {
            ObservableCollection<ChartClass> _Data;
            switch (iData)
            {
                case 1:
                default:
                    S1 = DisplayName;
                    IsS1 = true;
                    SeriesData1.SeriesDisplayName = S1;
                    _Data = DATA1;
                    break;
                case 2:
                    S2 = DisplayName;
                    IsS2 = true;
                    SeriesData2.SeriesDisplayName = S2;
                    _Data = DATA2;
                    break;
                case 3:
                    S3 = DisplayName;
                    IsS3 = true;
                    SeriesData3.SeriesDisplayName = S3;
                    _Data = DATA3;
                    break;
                case 4:
                    S4 = DisplayName;
                    IsS4 = true;
                    SeriesData4.SeriesDisplayName = S4;
                    _Data = DATA4;
                    break;
                case 5:
                    S5 = DisplayName;
                    IsS5 = true;
                    SeriesData5.SeriesDisplayName = S5;
                    _Data = DATA5;
                    break;
            }
            //Series.Clear(); 
            
            Refresh(iData, _Data, tb, X, Y);
            CheckData();
        }
        public virtual void Refresh(int iData, ObservableCollection<ChartClass> _Data, DataTable tb, string X, string Y)
        {
            try
            {
                if (tb == null)
                    return;
                _Data.Clear();
                ChartClass ChartClass;
                var qData = from XD in tb.AsEnumerable()
                            group XD by new
                          {
                              KEY = XD[X].ToString(),
                          }
                                into g
                                select new
                                {
                                    KEY = g.Key,
                                    YMAX = g.Max(XD => Convert.ToDouble(XD[Y].ToString())),
                                    YMIN = g.Min(XD => Convert.ToDouble(XD[Y].ToString())),
                                    YMID = g.Average(XD => Convert.ToDouble(XD[Y].ToString())),
                                    YQTY = g.Sum(XD => Convert.ToDouble(XD[Y].ToString())),
                                };
                double MAX = 0;
                foreach (var XD in qData)
                {
                    string key = (string)XD.KEY.KEY;
                    double YMAX = Helpers.Helper.RoundX(XD.YMAX * Converter * RATIO, 1);
                    if (YMAX > MAX)
                        MAX = YMAX;
                }
                
                foreach (var XD in qData)
                {
                    string key = (string)XD.KEY.KEY;
                    double YMAX = Helpers.Helper.RoundX(XD.YMAX * Converter * RATIO, 1);
                    double value = Helpers.Helper.RoundX(XD.YQTY * Converter * RATIO, 1);
                    if (value == 0)
                        continue;
                    if (value < MAX * limitation)
                        continue;
                    bool IsHas = false;
                    var query = from ChartClass data in _Data
                                where data.Category == key
                                select data;

                    foreach (ChartClass data in query)
                    {
                        IsHas = true;
                        data.Number = value;
                    }
                    if (!IsHas)
                    {
                        ChartClass = new ChartClass();
                        ChartClass.Category = key;
                        ChartClass.Number = value;
                        _Data.Add(ChartClass);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                
            }
        }
        public virtual void Confirm()
        {
            try
            {
                if (SeriesData5.SeriesDisplayName == "S5")
                {
                    if(Series.Count>5)
                    {
                        Series.RemoveAt(4);
                    }
                    
                }
                if (SeriesData4.SeriesDisplayName == "S4")
                {
                    if (Series.Count >4)
                    {
                        Series.RemoveAt(3);
                    }
                }
                if (SeriesData3.SeriesDisplayName == "S3")
                {
                    if (Series.Count > 3)
                    {
                        Series.RemoveAt(2);
                    }
                }
                if (SeriesData2.SeriesDisplayName == "S2")
                {
                    if (Series.Count > 2)
                    {
                        Series.RemoveAt(1);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        DispatcherTimer timer = new DispatcherTimer();
        int i = 0;
        Random rand = new Random();
        void timer_Tick(object sender, EventArgs e)
        {
            if (CanUpdate)
            {
                if (_DATA1.Count > MaxData)
                    _DATA1.RemoveAt(0);
                ChartClass ChartClass = new ChartClass();
                ChartClass.Category = "20" + i.ToString();
                ChartClass.Number = rand.Next(100, 200);
                _DATA1.Add(ChartClass);
                i++;
            }
        }
    }
}
